package net.maku.events.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.events.convert.UserContestQuestionConvert;
import net.maku.events.entity.UserContestQuestionEntity;
import net.maku.events.service.UserContestQuestionService;
import net.maku.events.query.UserContestQuestionQuery;
import net.maku.events.vo.UserContestQuestionVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 用户所答竞赛
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@RestController
@RequestMapping("events/user_contest_question")
@Tag(name = "用户所答竞赛")
@AllArgsConstructor
public class UserContestQuestionController {
    private final UserContestQuestionService userContestQuestionService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('events:user_contest_question:page')")
    public Result<PageResult<UserContestQuestionVO>> page(@ParameterObject @Valid UserContestQuestionQuery query) {
        PageResult<UserContestQuestionVO> page = userContestQuestionService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('events:user_contest_question:info')")
    public Result<UserContestQuestionVO> get(@PathVariable("id") Long id) {
        UserContestQuestionEntity entity = userContestQuestionService.getById(id);

        return Result.ok(UserContestQuestionConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('events:user_contest_question:save')")
    public Result<String> save(@RequestBody UserContestQuestionVO vo) {
        userContestQuestionService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('events:user_contest_question:update')")
    public Result<String> update(@RequestBody @Valid UserContestQuestionVO vo) {
        userContestQuestionService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('events:user_contest_question:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        userContestQuestionService.delete(idList);

        return Result.ok();
    }
}